package com.lxd.sunso.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxd.sunso.common.utils.DateUnit;
import com.lxd.sunso.product.entity.AlarmhisdataEntity;
import com.lxd.sunso.product.entity.AlarmregisterEntity;
import com.lxd.sunso.product.entity.MachineEntity;
import com.lxd.sunso.product.entity.MachinekindEntity;
import com.lxd.sunso.product.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class AsynServiceImpl implements AsynService {
    @Autowired
    private AlarmregisterService alarmregisterService;

    @Autowired
    private AlarmhisdataService alarmhisdataService;

    @Autowired
    private MachineService machineService;

    @Autowired
    private MachinekindService machinekindService;

    @Override
    @Async
    public void alarmProcess(MachineEntity machineEntity, HashMap<String, String> map) {
        //将数据与监控登记点的数据进行比较
        List<AlarmregisterEntity> entities = alarmregisterService.list(new QueryWrapper<AlarmregisterEntity>().eq("MachineId", machineEntity.getId()));
        if (entities == null) {
            return;
        }
        for (AlarmregisterEntity entity : entities) {
            boolean flag = false;
            String realtimeValue = map.get(entity.getDatatemplateid());
            //获取到实时数据后完成对比的功能
            //如果只有一个条件的话
            if (StringUtils.isEmpty(entity.getAndor())) {
                flag = compare(entity.getCondition1(), entity.getValue1(), realtimeValue);
            }
            //如果有两个条件而且第二个条件为与
            else if (entity.getAndor().equals("1")) {
                boolean b1 = compare(entity.getCondition1(), entity.getValue1(), realtimeValue);
                boolean b2 = compare(entity.getCondition2(), entity.getValue2(), realtimeValue);
                flag = b1 && b2;
            }
            //如果有两个条件而且第二个条件为或
            else if (entity.getAndor().equals("0")) {
                boolean b1 = compare(entity.getCondition1(), entity.getValue1(), realtimeValue);
                boolean b2 = compare(entity.getCondition2(), entity.getValue2(), realtimeValue);
                flag = b1 || b2;
            }

            if (flag) {
                AlarmhisdataEntity alarmhisdataEntity = new AlarmhisdataEntity();
                BeanUtils.copyProperties(entity, alarmhisdataEntity);
                alarmhisdataEntity.setDatavalue(map.get(entity.getDatatemplateid()));
                alarmhisdataEntity.setCreatetime(DateUnit.dateFormat(new Date()));
                alarmhisdataService.save(alarmhisdataEntity);

                if (entity.getIsonwarning().equals("0")) {
                    MachinekindEntity machinekindEntity = machinekindService.getById(machineEntity.getMachinekind());
                    String[] options = entity.getIssendmessage().split(",");
                    for (String option : options) {
                        switch (option) {
                            case "1": {
                                //获取到需要发送信息的邮箱
                                List<String> emails = machineService.getEmail(machineEntity.getMachinename());
                                //获取到需要发送的提示信息
                                String content = "设备" + machineEntity.getMachinename() + ":" + entity.getAlarmcontent() + "异常值为：" + realtimeValue;
                                //循环发送信息
                                for (String email : emails) {
                                    try {
                                        alarmregisterService.sendEmail(email, content);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                                break;
                            }
                            case "2": {
                                //获取到需要发送信息的手机号码
                                List<String> phones = machineService.getPhone(machineEntity.getMachinename());
                                //获取到需要发送的提示信息
                                String content = "设备" + machineEntity.getMachinename() + ":" + entity.getAlarmcontent() + "异常值为：" + realtimeValue;
                                //循环发送信息
                                for (String phone : phones) {
                                    try {
                                        alarmregisterService.sendMessage(phone, machinekindEntity.getMachinenumber(), machinekindEntity.getMachinename(), machineEntity.getMachinename(), content);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                                break;
                            }

                            case "3": {
                                System.out.println("发送强技通");
                                break;
                            }

                            case "4": {
                                System.out.println("发送微信");
                                break;
                            }

                            case "5": {
                                System.out.println("其他平台");
                                break;
                            }
                        }
                    }
                    entity.setIsonwarning("1");
                    alarmregisterService.updateById(entity);
                }
            } else {
                if (entity.getIsonwarning().equals("1")) {
                    entity.setIsonwarning("0");
                    alarmregisterService.updateById(entity);
                }
            }
        }
    }

    /**
     * @param condition1    条件
     * @param value         条件值
     * @param realtimeValue 实时数据值
     * @return
     */
    public boolean compare(String condition1, String value, String realtimeValue) {
        //定义flag判断
        boolean flag = false;
        //条件值
        Float f1 = Float.valueOf(realtimeValue);
        //实时数据的值
        Float f2 = Float.valueOf(value);
        switch (condition1) {
            case "1":
                flag = (f1 == f2);
                break;
            case "2":
                flag = (f1 != f2);
                break;
            case "3":
                flag = f1 > f2;
                break;
            case "4":
                flag = f1 >= f2;
                break;
            case "5":
                flag = f1 < f2;
                break;
            case "6":
                flag = f1 <= f2;
                break;
        }
        return flag;
    }
}
